import sys, random, os;
from Numeric import *;

top_dir = '/home/DBGROUP/svilen/exp/top';
tree_dir = '/home/DBGROUP/svilen/exp/trees';

top = {
	'100-1-sq': 100, '100-1-sq-broken-10-1': 100, '100-1-sq-broken-21-1': 100,
   	'100-n-1-1': 100, '100-n-1-2': 100, '100-n-1-3': 100, 
   	'100-n-2-1': 100, '100-n-2-2': 100, '100-n-2-3': 100,
   	'100-n-3-1': 100, '100-n-3-2': 100, '100-n-3-3': 100, 
   	'100-n-4-1': 100, '100-n-4-2': 100, '100-n-4-3': 100,
   	'250-n-1': 250, '200-n-1': 200, '50-n-1': 50, '55-intel': 55
      };

cf = {
	'node_topology':		top_dir+'/errors-100-n-4-1.txt',
	'dir_value': 			top_dir+'/values-100-n-4-1',
	'dir_tree': 			'trees',
	'dir_join': 			'joins',
	'dir_route': 			'route',
	'node_count': 			'100',
	'attributes': 			'B0 B0 B0 B0 E1 C10 E1 E1 R0',
	'dir_sample': 			top_dir+'/samples',
	'load_trees': 			'0',
	'route_begin': 			'0',
	'route_querytype': 		'2',
	'route_method': 		'1',
	'route_treesenabled': 		'3',
	'route_disableshortcuts': 	'0',
	'route_disablepayload': 	'0',
	'route_args': 			'25 50',
	'query_invjoinsel': 		'10',
	'query_invsrcsel': 		'2 2',
	'query_samplingtime': 		'0 10000',
	'query_sendtoroot': 		'0',
	'query_prune': 			'0',
	'query_disableintersect': 	'1',
	'query_multicast': 		'1',
	'query_cachemulticast': 	'1',
	'query_samplecycles': 		'100',
	'pause': 			'0',
	'disable_msglog': 		'0',
	'query_populatewindow':         '0',
        'query_windowsize':             '5',
        'retry_count':                  '200',
        'merge_payload':                '1',
        'merge_paths':                  '0',
        'config_enableght':             '0',
        'query_dimensions':             '60 60'
     };


def execute_config(cf):
        f = open('config.txt', 'w');
        for k, v in cf.iteritems():
                f.write(k+'='+v+'\n');
        f.close();
        
	os.system('python /home/DBGROUP/svilen/exp/source/build/micaz/test_script.py > debug_out.txt');
        os.system('mv debug_out.txt msg_rcv.log cmd_rcv.log msg_sent.log cmd_sent.log cmd_sentconf.log cmdhi_sent.log cmdhi_rcv.log '+cf['dir_route']+'/');


def build_trees(test_topologies):
	for top_name, n in top.iteritems():
		if top_name not in test_topologies:
			continue;

	        cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
        	cf['node_count'] = str(n);
	        cf['dir_value'] = top_dir+'/values-'+top_name;
	        cf['dir_tree'] = 'trees-'+top_name;

	        execute_config(cf);


def test_allpairroute(test_topologies, test_treesenabled, test_shortcuts, batch_size, batch_start, batch_end):
	for top_name, n in top.iteritems():
        	if top_name not in test_topologies:
                	continue;

	        cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
        	cf['node_count'] = str(n);
        	cf['dir_value'] = top_dir+'/values-'+top_name;
        	cf['dir_tree'] = tree_dir+'/trees-'+top_name;

        	cf['load_trees'] = '1';
	        cf['route_begin'] = '1';
	        cf['route_querytype'] = '0';
	        cf['route_method'] = '1';
	        cf['route_disablepayload'] = '1';

	        for cf['route_treesenabled'] in test_treesenabled:
	                for cf['route_disableshortcuts'] in test_shortcuts:
	                        random.seed(0);
	                        pair_used = zeros(n*n, Int);
        	                ca = '';

                	        for i in range(0, n*n):
                        	        k = random.randint(0, n*n-1);
                                	while pair_used[k] != 0:
                                        	k = random.randint(0, n*n-1);

	                        	pair_used[k] = 1;
	                                ca = ca+' '+str(k % n)+' '+str(k / n);

        	                        if i % batch_size == batch_size-1:
	                                        cf['route_args'] = ca;
        	                                cf['dir_route'] = 'allpair_route-'+top_name+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/route'+str(i/batch_size);

						if (batch_start < 0) | (batch_end < 0) | ((i/batch_size >= batch_start) & (i/batch_size <= batch_end)):
							print(top_name+' Batch '+str(i/batch_size)+' ('+cf['route_treesenabled']+', '+cf['route_disableshortcuts']+'): '+ca);
	                        	                execute_config(cf);

						ca = '';


def test_spatialroute(test_topologies, test_treesenabled, test_shortcuts):
        for top_name, n in top.iteritems():
		if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;

                cf['load_trees'] = '1';
                cf['route_begin'] = '1';
                cf['route_method'] = '1';
                cf['route_disablepayload'] = '1';

		cf['route_querytype'] = '1';
                for cf['route_treesenabled'] in test_treesenabled:
                        for cf['route_disableshortcuts'] in test_shortcuts:
				for test_index in (0, 1):
					cf['route_args'] = ('40 100', '0 12')[test_index];
					test_directory = 'spatial_route-'+top_name+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/q'+cf['route_querytype']+'/fringe'+str(test_index); 
					cf['dir_route'] = test_directory+'/route0';
					
					print('Executing: '+cf['dir_route']);
					execute_config(cf);
					os.system('~/analyze.exe '+test_directory);
					
		cf['route_querytype'] = '2';
                for cf['route_treesenabled'] in test_treesenabled:
                        for cf['route_disableshortcuts'] in test_shortcuts:
                                cf['route_args'] = '25 50';
                                test_directory = 'spatial_route-'+top_name+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/q'+cf['route_querytype'];
				cf['dir_route'] = test_directory+'/route0';

                                print('Executing: '+cf['dir_route']);
                                execute_config(cf);
				os.system('~/analyze.exe '+test_directory);


def set_joinquery():
                cf['route_querytype'] = '2';
                cf['route_args'] = '25 50';
#                cf['route_querytype'] = '0';
#                cf['route_args'] = '16 39 1 62 99 33 10 53 90 42 91 66 32 9 41 95 44 18 71 80';

                cf['query_populatewindow'] = '1';
                cf['query_windowsize'] = '3';

                cf['route_method'] = '1';
                cf['route_disablepayload'] = '0';
                cf['route_treesenabled'] = '3';
                cf['route_disableshortcuts'] = '0';


def test_joinmethods(test_topologies, srcsel_config, join_sel, test_methods):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
		cf['load_trees'] = '1';

		set_joinquery();

                for real_sel in srcsel_config:
                	cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

			join_config = (('1', '0', '0', '0'), ('0', '1', '0', '0'), ('0', '0', '0', '0'), ('0', '0', '1', '0'), ('0', '0', '1', '1'));
			for jointest_index in test_methods:
		                cf['query_sendtoroot'] = join_config[jointest_index][0];
        		        cf['query_prune'] = join_config[jointest_index][1];
	                	cf['query_multicast'] = join_config[jointest_index][2];
		                cf['query_cachemulticast'] = join_config[jointest_index][3];
			
				for cf['query_invjoinsel'] in join_sel:
					cf['dir_join'] = 'joinstate-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index);
					print('Executing: '+cf['dir_join']);

					cf['route_begin'] = '1';
					cf['dir_route'] = 'joinroute_est-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';

					print('Establishing paths...');
					execute_config(cf);

					cf['route_begin'] = '0';
                                        cf['dir_join'] = 'joinstate-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method0';
					cf['dir_route'] = 'joinroute_sampling-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';

					if (cf['query_sendtoroot'] == '1') & (real_sel[1] == '1'):
						cf['query_samplecycles'] = '200';
						cf['query_samplingtime'] = '0 20000';
					else:
						cf['query_samplecycles'] = '100';
                                                cf['query_samplingtime'] = '0 10000';

					print('Sampling...');
					execute_config(cf);


def test_joinvar(test_topologies, realsel_config, joinpointsel_config, join_sel):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
                cf['load_trees'] = '1';

		set_joinquery();

		cf['route_begin'] = '0';
                cf['query_sendtoroot'] = '0'; 
                cf['query_prune'] = '0';

#                cf['query_multicast'] = '0';
#                cf['query_cachemulticast'] = '0';
                cf['query_multicast'] = '1';
                cf['query_cachemulticast'] = '1';


		for cf['query_invjoinsel'] in join_sel:
			for joinpoint_sel in joinpointsel_config:
				cf['route_begin'] = '1';
				cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);
                		cf['dir_join'] = 'joinstate-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1]);
                	        cf['dir_route'] = 'joinroute_est-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/route0';

				print('Executing: '+cf['dir_route']);
	                        print('Establishing paths...');
        	                execute_config(cf);

				for real_sel in realsel_config:
					cf['route_begin'] = '0';
					cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);
	                                cf['dir_route'] = 'joinroute_sampling-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss_real'+str(real_sel[0])+'-'+str(real_sel[1])+'/ss_used'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/route0';

					print('Executing: '+cf['dir_route']);
	                                print('Sampling...');
        	                        execute_config(cf);



def treebuild_intel():
        for top_name, n in top.iteritems():
                if 'intel' not in top_name:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;

                execute_config(cf);


def route_intel(send_to_root):
	for top_name, n in top.iteritems():
                if 'intel' not in top_name:
			continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
  	
		cf['dir_join'] = 'joins-intel-q4';
		cf['dir_sample'] = top_dir+'/samples-intel'
		
		cf['load_trees'] = '1';
		cf['route_querytype'] = '3';
		cf['route_method'] = '1';
		cf['route_treesenabled'] = '3';
		cf['route_disableshortcuts'] = '0';
		cf['route_disablepayload'] = '0';
		cf['route_args'] = '10 10';

		cf['query_invjoinsel'] = '5';
		cf['query_invsrcsel'] = '1 1';
		cf['query_samplingtime'] = '0 60000';
		cf['query_sendtoroot'] = send_to_root;
		cf['query_disableintersect'] = '1';
		cf['query_multicast'] = '0';
		cf['query_prune'] = '0';
		cf['query_samplecycles'] = '100';
		cf['query_populatewindow'] = '0';
		cf['query_windowsize'] = '1';

                cf['route_begin'] = '1';
		cf['dir_route'] = 'route-intel-q4-est'
		print('Establishing paths...');		
		execute_config(cf);

                cf['route_begin'] = '0';
		cf['dir_route'] = 'route-intel-q4-join'
                print('Sampling...');
                execute_config(cf);


def test_allpairghtroute(test_topologies, dim, batch_size, batch_start, batch_end):
	for top_name, n in top.iteritems():
        	if top_name not in test_topologies:
                	continue;
	        cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
        	cf['node_count'] = str(n);
        	cf['dir_value'] = top_dir+'/values-'+top_name;
        	cf['dir_tree'] = tree_dir+'/trees-'+top_name;
        	cf['load_trees'] = '1';
	        cf['route_begin'] = '1';
	        cf['route_querytype'] = '0';
	        cf['route_method'] = '1';
	        cf['route_disablepayload'] = '1';
		cf['config_enableght'] = '1';
		cf['query_dimensions'] = dim;

	        random.seed(0);
	        pair_used = zeros(n*n, Int);
        	ca = '';

                for i in range(0, n*n):
               		k = random.randint(0, n*n-1);
                        while pair_used[k] != 0:
                        	k = random.randint(0, n*n-1);
	                
			pair_used[k] = 1;
	                ca = ca+' '+str(k % n)+' '+str(k / n);

        	        if i % batch_size == batch_size-1:
	                	cf['route_args'] = ca;
        	                cf['dir_route'] = 'allpair_route-'+top_name+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/route'+str(i/batch_size);
				
				if (batch_start < 0) | (batch_end < 0) | ((i/batch_size >= batch_start) & (i/batch_size <= batch_end)):
					print(top_name+' Batch '+str(i/batch_size)+' ('+cf['route_treesenabled']+', '+cf['route_disableshortcuts']+'): '+ca);
	                        	execute_config(cf);
				
				ca = '';
 

def test_ghtjoinvar(test_topologies, joinpointsel_config, join_sel, query_dim):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
                cf['load_trees'] = '1';

                set_joinquery();

                cf['route_begin'] = '0';
                cf['query_sendtoroot'] = '0';
                cf['query_prune'] = '0';

		cf['config_enableght'] = '1';
		cf['query_dimensions'] = query_dim;

                for cf['query_invjoinsel'] in join_sel:
			 for joinpoint_sel in joinpointsel_config:
	                	cf['route_begin'] = '1';
        	                cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);
                		cf['dir_join'] = 'joinstate-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1]);
                	        cf['dir_route'] = 'joinroute_est-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/route0';

 	                        print('Executing: '+cf['dir_route']);
        	                print('Establishing paths...');
                	        execute_config(cf);

                        	cf['route_begin'] = '0';
	                        cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);
	                        cf['dir_route'] = 'joinroute_sampling-var-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/route0'; 

                	        print('Executing: '+cf['dir_route']);
                        	print('Sampling...');
	                        execute_config(cf);


def test_repairest(test_topologies, srcsel_config, join_sel, test_methods, fail_count, fail_nodes):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
                cf['load_trees'] = '1';

		cf['retry_count'] = '20';

                set_joinquery();

                for real_sel in srcsel_config:
                        cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                        join_config = (('1', '0', '0', '0'), ('0', '1', '0', '0'), ('0', '0', '0', '0'), ('0', '0', '1', '0'), ('0', '0', '1', '1'));
                        for jointest_index in test_methods:
                                cf['query_sendtoroot'] = join_config[jointest_index][0];
                                cf['query_prune'] = join_config[jointest_index][1];
                                cf['query_multicast'] = join_config[jointest_index][2];
                                cf['query_cachemulticast'] = join_config[jointest_index][3];

                                for cf['query_invjoinsel'] in join_sel:
                                        print('Establishing paths...');
                                        for fail_test in range(0, fail_count+1):
                                                cf['fail_nodes'] = '';
                                                ft = 0;

                                                for node_id, fail_time in fail_nodes.iteritems():
                                                        if (ft == fail_test):
                                                                break;
                                                        ft = ft+1;
                                                        cf['fail_nodes'] = cf['fail_nodes']+' '+str(node_id)+':'+str(fail_time);

						cf['dir_join'] = 'joinstate-'+top_name+'/f'+str(fail_test)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index); 
	                                        print('Executing: '+cf['dir_join']);

        	                                cf['route_begin'] = '1';
						cf['dir_route'] = 'joinroute_est-'+top_name+'/f'+str(fail_test)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';

						print('Failed nodes: '+cf['fail_nodes']);
                                        	execute_config(cf);


def test_repairsampling(test_topologies, srcsel_config, join_sel, test_methods, only_est, fail_joinnode):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
                cf['dir_tree'] = tree_dir+'/trees-'+top_name;
                cf['load_trees'] = '1';

		cf['retry_count'] = '20';

                set_joinquery();
                cf['route_querytype'] = '0';
                cf['route_args'] = '44 47';

                for real_sel in srcsel_config:
                        cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                        join_config = (('1', '0', '0', '0'), ('0', '1', '0', '0'), ('0', '0', '0', '0'), ('0', '0', '1', '0'), ('0', '0', '1', '1'));
                        for jointest_index in test_methods:
                                cf['query_sendtoroot'] = join_config[jointest_index][0];
                                cf['query_prune'] = join_config[jointest_index][1];
                                cf['query_multicast'] = join_config[jointest_index][2];
                                cf['query_cachemulticast'] = join_config[jointest_index][3];

                                for cf['query_invjoinsel'] in join_sel:
					cf['dir_join'] = 'joinstate-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index);

					if (only_est == 1):
                                                cf['route_begin'] = '1';
                                                cf['fail_nodes'] = '';
						cf['dir_route'] = 'joinroute_est-'+top_name+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';					
                                        
						print('Executing: '+cf['dir_route']);
						execute_config(cf);

						continue;
					
					cf['route_begin'] = '0';
					cf['fail_nodes'] = '';
					cf['dir_route'] = 'joinroute_sampling-'+top_name+'/f0'+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';

                                        print('Executing: '+cf['dir_route']);
                                        execute_config(cf);

					for fail_test in range(0, 10):
						cf['fail_nodes'] = fail_joinnode[cf['query_invjoinsel']]+':'+str(4500+fail_test*100);
						cf['dir_route'] = 'joinroute_sampling-'+top_name+'/f'+str(fail_test+1)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/route0';
	        
		                                print('Executing: '+cf['dir_route']);
						print('Failed nodes: '+cf['fail_nodes']);
        	                                execute_config(cf);

 
